<script setup>
import { ref } from "vue";
import router from "../../router"; // 引入路由配置
import { showDialog } from 'vant';
import axios from 'axios'
import { showNotify } from 'vant';

const username = ref("");
const password = ref("");
const passwordRepeat = ref("");
const onSubmit = (values) => {
  console.log("submit", values);
  // 判断两次密码是否相同
  if (values.password != values.passwordRepeat) {
    showDialog({
      message: "两次密码输入不相同，请重新输入，谢谢！",
    }).then(() => {
      // on close
      password.value = "";
      passwordRepeat.value = "";
    });
  }else{
    let data = {
      username: values.name,
      password: values.password
    }
    if(data.password.length >= 6){
      axios.post("/api/user/enroll", data).then(res => {
        console.log(res);
        showNotify({ type: 'primary', message: res.data.msg });
        if(res.data.err == 200){
          router.push("/login")
        }
      })
    }else{
      showNotify({ type: 'primary', message: "密码位数设置不足" });
    }
    
  }
};

const register = () => {
  router.push("/register");
};
</script>

<template>
  <div>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="username"
          name="name"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <van-field
          v-model="passwordRepeat"
          type="password"
          name="passwordRepeat"
          label="确认密码"
          placeholder="确认密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button round block type="primary" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<style scoped></style>
